//
//  ViewController2.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 1. 程序载入的时候读取更新界面信息
    [self updateInfo];
    // 2. 监听程序进入前台的通知,更新界面信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSLog(@"%s",__func__);
    // 3. 第二个页面回来的时候，更新下界面信息
    [self updateInfo];
}
- (void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (IBAction)backBtnPressed:(id)sender {
    // 关闭模态窗口
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)updateInfo
{
    // 从NSUserDefaults中使用指定的key将信息读取处理
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    self.slider1.value = [d floatForKey:@"redSlider"];
    self.slider2.value = [d floatForKey:@"greenSlider"];
    self.slider3.value = [d floatForKey:@"blueSlider"];
    
    self.tv1.text = [NSString stringWithFormat:@"%.2f",self.slider1.value];
    self.tv2.text = [NSString stringWithFormat:@"%.2f",self.slider2.value];
    self.tv3.text = [NSString stringWithFormat:@"%.2f",self.slider3.value];
    
    self.imageView.backgroundColor = [UIColor colorWithRed:_slider1.value green:_slider2.value blue:_slider3.value alpha:1];
}

@end
